﻿<div class="form-field">
	<#if form.mode == "view">
		<table>
		<div class="viewmode-field">
			<#if field.mandatory && !(field.value?is_number) && field.value == "">
				<span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
			</#if>
			<span class="viewmode-label">${field.label?html}:</span>
			<#if field.control.params.activateLinks?? && field.control.params.activateLinks == "true">
				<#assign fieldValue=field.value?html?replace("((http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?\\^=%&:\\/~\\+#]*[\\w\\-\\@?\\^=%&\\/~\\+#])?)", "<a href=\"$1\" target=\"_blank\">$1</a>", "r")>
			<#else>
				<#if field.value?is_number>
					<#assign fieldValue=field.value?c>
				<#else>
					<#assign fieldValue=field.value?html>
				</#if>
			</#if>
			<span class="viewmode-value"><#if fieldValue == "">${msg("form.control.novalue")}<#else>${fieldValue}</#if></span>
		</div>
		<#else>
			<tr>
				<td>
					<!-- <label for="signPdfButton">Pasirašyti dokumentą(-us)</label>-->
					<span class="yui-button yui-push-button">
					<span class="first-child">
						<button id="signPdfButton" type="button" name="signPdfButton" onclick="signPdf()" tabindex="0">Pasirašyti dokumentą (-us) tik stacionariu elektroniniu parašu</button>
					</span></span>
				</td>
			</tr>
			<tr>
				<td>
					<div id="responseStatus"></div> <br />
				</td>
			</tr>
		</table>
	</#if>
</div>

<script>
function signPdf(){
	var aTaskId = null;
	aTaskId = getURLParameters('taskId');
	if (aTaskId != null) {
		postToSigning(aTaskId);
	}
}

function postToSigning(taskId){
    var taskId = taskId || "";
    var signingUrl = "/share/proxy/alfresco/registrucentras/workflow_multisign";
    var params = {};
    if (taskId != ""){
        params["taskId"] = taskId;
    }
    post_to_url(signingUrl, params);
}

function post_to_url(path, params) {
    method = "post"; // Set method to post by default, if not specified.
    params = params || {};

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
	//form.setAttribute("target", '_blank');

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }
    }
    document.body.appendChild(form);
    form.submit();
}

// get param value from URL
function getURLParameters(paramName) {
	var sURL = window.document.URL.toString();  
    if (sURL.indexOf("?") > 0) {
		var arrParams = sURL.split("?");         
		var arrURLParams = arrParams[1].split("&");      
		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);     
		var i = 0;
		for (i = 0; i < arrURLParams.length; i++) {
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "") {
				arrParamValues[i] = unescape(sParam[1]);
			} else {
				arrParamValues[i] = "No Value";
			}
		}
		for (i = 0; i < arrURLParams.length; i++) {
			if(arrParamNames[i] == paramName){
				return arrParamValues[i];
			}
		}
		return "Parametras nerastas";
    }
}
</script>